package com.example.test.advice.第九章_多线程和并发.建议123_Voliate不能保证数据同步;

public class Client {
    public static void main(String[] args) {
        //理想值，并作为最大循环此处
        int value = 1000;
        //循环次数,防止无限循环出现死机现象
        int loops = 0;
        //主线程组，用于估计活动线程数
        ThreadGroup tg = Thread.currentThread().getThreadGroup();
        while (loops++<value){
            System.out.println("222222222");
            //共享资源清零
            UnsafeThread ut = new UnsafeThread();
            for (int i = 0;i<value;i++){
                new Thread(ut).start();
            }
            //先等15毫秒，等待线程数量变为1
            do {
                try {
                    //System.out.println("activeCount:"+tg.activeCount()+",loops:"+loops);
                    Thread.sleep(15);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }while (tg.activeCount() != 2);
            if(ut.getCount()!=value){
                //出现线程不安全的情况
                System.out.println("循环到第"+loops+"遍，出现线程不全情况");
                System.out.println("此时,count="+ut.getCount());
                System.exit(0);
            }else {
                System.out.println("没出现！");
            }

        }
    }
}
